package com.cz.nongyetongv5_1.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils.TruncateAt;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cz.nongyetongv5.bean.TextInfo;
import com.cz.nongyetongv5.custom.wheelview.Utils;

public class ListAdapter extends BaseAdapter {
    /**
     * 数据集
     */
    public  void setSelectItem(int selectItem) {
        this.selectItem = selectItem;
    }
    private int  selectItem=-1;
    private ArrayList<TextInfo> mData = null;
    /**
     * 控件的宽度
     */
    private int mWidth = ViewGroup.LayoutParams.MATCH_PARENT;

    /**
     * 控件的高度,默认为50
     */
    //private int mHeight = 50;

    /**
     * 上下文对象
     */
    private Context mContext = null;

    public ListAdapter(Context context) {
        mContext = context;
        // 根据密度计算高度
        //mHeight = (int) Utils.pixelToDp(context, mHeight);
    }

    public void setData(ArrayList<TextInfo> data) {
        mData = data;

        // 刷新WheelView
        this.notifyDataSetChanged();
    }

    public void setItemSize(int width, int height) {
        mWidth = width;
        //mHeight = (int) Utils.pixelToDp(mContext, height);
    }

    @Override
    public int getCount() {
        int count = (null != mData) ? mData.size() : 0;

        return count;
    }

    @Override
    public Object getItem(int position) {
        if(position>=mData.size()){
            return null;
        }
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public int getPosition(Object item) {

        return this.mData.indexOf(item);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {



        TextView textView = null;

        TextInfo info = mData.get(position);

        if (null == convertView) {
            textView = new TextView(mContext);
        } else {
            // 视图缓冲区取数据
            textView = (TextView) convertView;
        }

        textView.setTextSize(22);

        textView.setSingleLine(true);

        textView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

        int padding = Utils.dip2px(mContext, 8);

        textView.setPadding(0, padding, 0, padding);

        textView.setTextColor(Color.parseColor("#36495C"));

        textView.setText(info.mText);
        textView.setEllipsize(TruncateAt.MARQUEE);
        if (position == selectItem) {
            textView.setBackgroundColor(Color.parseColor("#e2e2e2"));
        }
        else {
            textView.setBackgroundColor(Color.TRANSPARENT);
        }

        return textView;
    }

}
